package com.jian.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.jetbrains.annotations.NotNull;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @author Jiange
 * @create 2021-04-06-17:39
 */
public class JDBCUtils {

    private static DataSource dataSource;

    static {
        Properties properties = new Properties();
        try {
            properties.load(JDBCUtils.class.getClassLoader().getResourceAsStream("JDBC.properties"));
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取数据库连接池中的连接
     * @return
     */
    public static Connection getConnection() throws SQLException {
        Connection connection = null;
        connection = dataSource.getConnection();
        return connection;
    }

    /**
     * 关闭连接 放回连接池
     * @param connection
     */
    public static void closeConnection(@NotNull Connection connection) throws SQLException {
        connection.close();
    }
}
